package com.itheima.dubbo.web;

import com.itheima.dubbo.User;
import com.itheima.dubbo.UserFace;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    // 远程注入， 接口dubbo会为其实现代理对象 reference 引用
//    @DubboReference
    // 启动检查 禁用启动检查, 防止报错, 只再test阶段使用
//    @DubboReference(check = false)
    // 负载均衡调用, 修改为：roundrobin 加权轮询
    @DubboReference(loadbalance = "roundrobin")
    // 集群容错
//    @DubboReference(cluster = "failfast", retries = 4)
    // dubbo 直连 服务提供方ip和服务提供方dubbo端口号
//    @DubboReference(url = "192.168.1.106:20880")
    // 灰度发布
//    @DubboReference(version = "2.0.0")
    // 超时时间
//    @DubboReference(timeout = 100)
    // 远程注入， 接口dubbo会为其实现代理对象
//    @DubboReference(check = false, methods = {
//            @Method(name = "findById",timeout = 2000),
//            @Method(name = "hello",timeout = 10000),
//    })
    private UserFace userFace;

    @GetMapping("/hello")
    public String hello(String username) {
        String result = userFace.hello(username);
        return result;
    }


    @GetMapping("/findOne")
    public User findOne(Long id) {
        RpcContext context = RpcContext.getContext();
        context.setAttachment("it", "Shanghai heima");
        User user = userFace.findById(new User(id, "上海"));
        return user;
    }
}
